using UnityEngine;

public class ChatController : MonoBehaviour
{
	public static ChatController thisScript;

	private TouchScreenKeyboard mKeyboard;

	private string mText = string.Empty;

	public int maxChars = 40;

	public bool isHold = true;

	public AudioClip sendChatClip;

	[HideInInspector]
	public UILabel poleVvoda;

	private bool sent = true;

	private int messNumber;

	private void Awake()
	{
		thisScript = this;
	}

	private void Start()
	{
		if (TutorialManager.TuturialWorking())
		{
			base.enabled = false;
		}
		else
		{
			poleVvoda = ChatWindow.instance.poleVvoda;
		}
	}

	private void Update()
	{
		if (mKeyboard == null)
		{
			return;
		}
		string text = mKeyboard.text;
		if (mText != text)
		{
			mText = string.Empty;
			foreach (char c in text)
			{
				if (c != 0)
				{
					mText += c;
				}
			}
			if (maxChars > 0 && mKeyboard.text.Length > maxChars)
			{
				mKeyboard.text = mKeyboard.text.Substring(0, maxChars);
			}
			if (mText != text)
			{
				mKeyboard.text = mText;
			}
		}
		poleVvoda.text = mText;
		if (mKeyboard.done && !mKeyboard.wasCanceled)
		{
			if (!poleVvoda.text.Equals(string.Empty) && !sent)
			{
				postMessageToChat();
				poleVvoda.text = string.Empty;
				mText = string.Empty;
				mKeyboard.text = string.Empty;
				sent = true;
			}
			BCWDebug.Log("pressDone " + mText);
			if (isHold)
			{
				mKeyboard.active = true;
			}
		}
		else if (!mKeyboard.wasCanceled)
		{
		}
		if (mKeyboard != null && mKeyboard.active)
		{
		}
	}

	public void postMessageToChat()
	{
		if (BCWDebug.isDebugBuild)
		{
			BCWDebug.Log("post to chat:" + settings.tekName + ":" + poleVvoda.text);
		}
		if (VolumeController.IsSoundEnable)
		{
			NGUITools.PlaySound(sendChatClip);
		}
		ChatWindow.instance.chatRoom.AddMessageToScroll(poleVvoda.text, true, PunTeams.Team.none, ChatWindow.IdChatType.general);
		GameController.instance.addMessageToListOnline(settings.tekName + ": " + poleVvoda.text);
	}

	public void postMessageToChat(string polevvoda, PunTeams.Team team, ChatWindow.IdChatType idChat)
	{
		if (BCWDebug.isDebugBuild)
		{
			BCWDebug.Log("post to chat:" + settings.tekName + ":" + polevvoda);
		}
		if (VolumeController.IsSoundEnable)
		{
			NGUITools.PlaySound(sendChatClip);
		}
		ChatWindow.instance.chatRoom.AddMessageToScroll(polevvoda, true, team, idChat);
		GameController.instance.addMessageToListOnline(settings.tekName + ": " + polevvoda, 0, team, idChat);
	}

	public void createChat()
	{
		if (poleVvoda != null)
		{
			poleVvoda.text = string.Empty;
		}
		base.enabled = true;
		mKeyboard = TouchScreenKeyboard.Open(string.Empty, TouchScreenKeyboardType.Default, false, false);
		sent = false;
	}

	public void closeChat()
	{
		if (BCWDebug.isDebugBuild)
		{
			BCWDebug.Log("closeChat");
		}
		if (mKeyboard != null)
		{
			mKeyboard.active = false;
			mKeyboard = null;
		}
		base.enabled = false;
	}

	private void CloseChatWindow()
	{
		WindowManager.instance.HideWindow(typeof(ChatWindow));
		WindowManager.instance.ShowWindow(typeof(GameTopWindow), true);
	}

	private void OnDestroy()
	{
		closeChat();
	}
}
